cmake_minimum_required(VERSION 3.27)

# Roots
include("../roots.cmake")

set(CONTEXT rtos.Debug+Target_1)
set(TARGET ${CONTEXT})
set(OUT_DIR "${SOLUTION_ROOT}/out/rtos/Target_1/Debug")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(LD_SCRIPT "${SOLUTION_ROOT}/rtos_Target_1.sct")
set(LD_SCRIPT_PP ${LD_SCRIPT})

# Processor Options
set(CPU Cortex-M4)
set(FPU SP_FPU)
set(BYTE_ORDER Little-endian)

# Toolchain config map
include("toolchain.cmake")

# Setup project
project(${CONTEXT} LANGUAGES C ASM)

# Compilation database
add_custom_target(database
  COMMAND ${CMAKE_COMMAND} -E make_directory "${OUT_DIR}"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json" "${OUT_DIR}"
)
set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})

# Setup context
add_executable(${CONTEXT})
set_target_properties(${CONTEXT} PROPERTIES PREFIX "" SUFFIX ".axf" OUTPUT_NAME "rtos")
set_target_properties(${CONTEXT} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${OUT_DIR})
add_library(${CONTEXT}_GLOBAL INTERFACE)

# Includes
target_include_directories(${CONTEXT} PUBLIC
  $<$<COMPILE_LANGUAGE:C,CXX>:
    "${SOLUTION_ROOT}/rtthread/bsp"
    "${SOLUTION_ROOT}/rtthread/include"
    "${SOLUTION_ROOT}/.."
  >
  "${SOLUTION_ROOT}/RTE/_Debug_Target_1"
  "${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include"
  "${CMSIS_PACK_ROOT}/GigaDevice/GD32F4xx_DFP/3.3.0/Device/F4XX/Firmware/Peripherals/inc"
  "${CMSIS_PACK_ROOT}/GigaDevice/GD32F4xx_DFP/3.3.0/Device/F4XX/Include"
  "${SOLUTION_ROOT}/rtthread/components/finsh"
)

# Defines
target_compile_definitions(${CONTEXT} PUBLIC
  $<$<COMPILE_LANGUAGE:C,CXX>:
    GD32F470
    _RTE_
  >
)

# Compile options
target_compile_options(${CONTEXT} PUBLIC
  $<$<COMPILE_LANGUAGE:ASM>:
    "SHELL:${ASM_CPU}"
    "SHELL:${ASM_FLAGS}"
    "SHELL:${ASM_BYTE_ORDER}"
    "SHELL:-masm=auto"
  >
  $<$<COMPILE_LANGUAGE:C>:
    "SHELL:${CC_CPU}"
    "SHELL:${CC_FLAGS}"
    "SHELL:${CC_BYTE_ORDER}"
    "SHELL:-std=c99"
    "SHELL:-fno-rtti"
    "SHELL:-funsigned-char"
    "SHELL:-fshort-enums"
    "SHELL:-fshort-wchar"
    "SHELL:-ffunction-sections"
    "SHELL:-Wall"
    "SHELL:-Wextra"
    "SHELL:-Wno-packed"
    "SHELL:-Wno-reserved-id-macro"
    "SHELL:-Wno-unused-macros"
    "SHELL:-Wno-documentation-unknown-command"
    "SHELL:-Wno-documentation"
    "SHELL:-Wno-license-management"
    "SHELL:-Wno-parentheses-equality"
    "SHELL:-DRT_USING_MEMHEAP_AS_HEAP"
  >
)

# Compile Options Abstractions
add_library(${CONTEXT}_ABSTRACTIONS INTERFACE)
cbuild_set_options_flags(CC "none" "on" "" "" CC_OPTIONS_FLAGS_${CONTEXT})
cbuild_set_options_flags(ASM "none" "on" "" "" ASM_OPTIONS_FLAGS_${CONTEXT})
target_compile_options(${CONTEXT}_ABSTRACTIONS INTERFACE
  $<$<COMPILE_LANGUAGE:C>:
    "SHELL:${CC_OPTIONS_FLAGS_${CONTEXT}}"
  >
  $<$<COMPILE_LANGUAGE:ASM>:
    "SHELL:${ASM_OPTIONS_FLAGS_${CONTEXT}}"
  >
)

# Add groups and components
include("groups.cmake")
include("components.cmake")

target_link_libraries(${CONTEXT} PUBLIC
  Group_Source_Group_1
  Group_rtthread
  ARM_CMSIS_CORE_6_1_0
  GigaDevice_Device_Startup_3_3_0
  GigaDevice_Device_StdPeripherals_ADC_3_3_0
  GigaDevice_Device_StdPeripherals_CAN_3_3_0
  GigaDevice_Device_StdPeripherals_CRC_3_3_0
  GigaDevice_Device_StdPeripherals_CTC_3_3_0
  GigaDevice_Device_StdPeripherals_DAC_3_3_0
  GigaDevice_Device_StdPeripherals_DBG_3_3_0
  GigaDevice_Device_StdPeripherals_DCI_3_3_0
  GigaDevice_Device_StdPeripherals_DMA_3_3_0
  GigaDevice_Device_StdPeripherals_ENET_3_3_0
  GigaDevice_Device_StdPeripherals_EXMC_3_3_0
  GigaDevice_Device_StdPeripherals_EXTI_3_3_0
  GigaDevice_Device_StdPeripherals_FMC_3_3_0
  GigaDevice_Device_StdPeripherals_FWDGT_3_3_0
  GigaDevice_Device_StdPeripherals_GPIO_3_3_0
  GigaDevice_Device_StdPeripherals_I2C_3_3_0
  GigaDevice_Device_StdPeripherals_IPA_3_3_0
  GigaDevice_Device_StdPeripherals_IREF_3_3_0
  GigaDevice_Device_StdPeripherals_MISC_3_3_0
  GigaDevice_Device_StdPeripherals_PMU_3_3_0
  GigaDevice_Device_StdPeripherals_RCU_3_3_0
  GigaDevice_Device_StdPeripherals_RTC_3_3_0
  GigaDevice_Device_StdPeripherals_SDIO_3_3_0
  GigaDevice_Device_StdPeripherals_SPI_I2S_3_3_0
  GigaDevice_Device_StdPeripherals_SYSCFG_3_3_0
  GigaDevice_Device_StdPeripherals_TIMER_3_3_0
  GigaDevice_Device_StdPeripherals_TLI_3_3_0
  GigaDevice_Device_StdPeripherals_TRNG_3_3_0
  GigaDevice_Device_StdPeripherals_USART_3_3_0
  GigaDevice_Device_StdPeripherals_WWDGT_3_3_0
)

# Linker options
target_link_options(${CONTEXT} PUBLIC
  "SHELL:${LD_CPU}"
  "SHELL:${_LS}\"${LD_SCRIPT_PP}\""
  "SHELL:--map"
  "SHELL:--load_addr_map_info"
  "SHELL:--xref"
  "SHELL:--callgraph"
  "SHELL:--symbols"
  "SHELL:--info sizes"
  "SHELL:--info totals"
  "SHELL:--info unused"
  "SHELL:--info veneers"
  "SHELL:--strict"
  "SHELL:--summary_stderr"
  "SHELL:--info summarysizes"
)
set_target_properties(${CONTEXT} PROPERTIES LINK_DEPENDS ${LD_SCRIPT})
